jQuery(document).ready(function () {
    selectProductList();
    $("#resetBtn").on("click",function () {
        $("#productName").val("");
        $("#productId").val("");
        $("#author").val("");
        $("#press").val("");
        $("#type").val("");
        $("#state").val("");
        $("#startTime").val("");
        $("#endTime").val("");
    });
});
function queryParams(params) {
    return getQueryParams(params, "searchForm");
}
function selectProductList() {
    $('#productTable').bootstrapTable("destroy");
    $('#productTable').bootstrapTable({
        method: "post",
        url: rootPath+"/productManage/ajax/getProductList",
        showExport : true,
        exportTypes: ['excel'],//['json', 'xml', 'csv', 'txt', 'sql', 'excel'],
        Icons:'glyphicon-export',
        exportOptions:{
            ignoreColumn: [0],  //忽略某一列的索引
            fileName: '产品列表',  //文件名称设置
            worksheetName: 'sheet1',  //表格工作区名称
            tableName: '产品列表'
        },
        exportDataType : 'all'
    });
}
function addProduct() {
    layer.open({
        type: 2,
        title: "添加产品",
        shadeClose: false,
        shade: 0.8,
        area: ['70%', '70%'],
        content: rootPath + "/productManage/toPage/productAddView?decorator=decorator_blank",
        maxmin: true
    });
}
function updProductInfo() {
    var selections = $('#productTable').bootstrapTable('getSelections');
    if (selections.length == 0) {
        layer.msg('请选择一行数据进行操作！', function () {
        });
        return;
    }
    if (selections[0].state == 0){
        layer.msg('不出售的产品不可以修改产品信息',function () {
        });
        return
    }
    layer.open({
        type: 2,
        title: "修改产品信息",
        shadeClose: false,
        shade: 0.8,
        area: ['70%', '70%'],
        content: rootPath + "/productManage/toPage/productUpdView?decorator=decorator_blank&productId="+selections[0].product_id,
        maxmin: true
    });
}
function delProduct() {
    var selections = $('#productTable').bootstrapTable('getSelections');
    if (selections.length == 0) {
        layer.msg('请选择一行数据进行操作！', function () {
        });
        return;
    }
    if (selections[0].state == 0){
        layer.msg('已废止的产品不可再次操作',function () {
        });
        return;
    }
    var productId = selections[0].product_id;
    layer.open({
        content: '确定废止此产品？',
        btn: ['确定', "取消"],
        shade: false,
        offset: '200px',
        yes: function (index, layero) {
            $.ajax({
                type: "POST",
                url: rootPath + "/productManage/ajax/delProduct",
                data:{productId:productId},
                dataType: "json",
                success : function(data) {
                    $("#productTable").bootstrapTable('refresh');//刷新表格
                    if(data.code == '0000') {
                        layer.msg(data.msg, {icon: 1});
                    }else {
                        layer.msg(data.msg, {icon: 2});
                    }
                },
                error:function(){
                    layer.msg("操作出错！",{icon: 2});
                }
            });
        },
        cancel: function (index) {
        }
    });
}
function doProductIn() {
    var selections = $('#productTable').bootstrapTable('getSelections');
    if (selections.length == 0) {
        layer.msg('请选择一行数据进行操作！', function () {
        });
        return;
    }
    if (selections[0].state != 0){
        layer.msg('在用的产品不可再次操作',function () {
        });
        return;
    }
    var productId = selections[0].product_id;
    layer.open({
        content: '确定重新启用此产品？',
        btn: ['确定', "取消"],
        shade: false,
        offset: '200px',
        yes: function (index, layero) {
            $.ajax({
                type: "POST",
                url: rootPath + "/productManage/ajax/doProductIn",
                data:{productId:productId},
                dataType: "json",
                success : function(data) {
                    $("#productTable").bootstrapTable('refresh');//刷新表格
                    if(data.code == '0000') {
                        layer.msg(data.msg, {icon: 1});
                    }else {
                        layer.msg(data.msg, {icon: 2});
                    }
                },
                error:function(){
                    layer.msg("操作出错！",{icon: 2});
                }
            });
        },
        cancel: function (index) {
        }
    });
}